package com.jade.zsy.base.facade.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.zsy.base.dao.StoreClassDao;
import com.jade.zsy.base.entity.StoreClass;
import com.jade.zsy.base.facade.StoreClassFacade;
import com.jade.zsy.base.vo.StoreClassVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/6/3
 * Time: 12:59
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
@Service("StoreClassFacade")
public class StoreClassFacadeImpl extends BaseFacadeImpl implements StoreClassFacade {
    @Inject
    @Named("StoreClassDao")
    private StoreClassDao entityDao;
    @Override
    @Transactional
    public void save(Object obj) throws DbException {
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        StoreClass entity=(StoreClass)obj;
        entity.setOwnerId(customer.getOwnerId());
        entity.setCreator(customer.getNickName());
        entity.setCreatorId(customer.getId());
        super.save(entity);
    }

    @Override
    @Transactional
    public void update(Object obj) throws DbException {
        StoreClass entity=(StoreClass)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        entity.setModifier(customer.getTrueName());
        super.update(entity);
    }

    @Override
    public PagingResult queryEntity(Object obj, int page, int size, String keys, String... orders) throws DbException,IllegalAccessException, InstantiationException {
        StoreClassVo entity=(StoreClassVo)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        Criteria criteria = new Criteria((page - 1) * size, size);
        criteria.add(Restrictions.eq("ownerId",customer.getOwnerId()));
        if(StringUtils.isNotEmpty(keys)){
            criteria.add(Restrictions.or(Restrictions.like("keywords",keys), Restrictions.eq("name",keys)));
        }

        PagingList pagingList = entityDao.queryEntity(StoreClass.class, criteria);
        PagingResult result = PagingResult.getResult(CopyBean.copy(pagingList, StoreClassVo.class), pagingList.getTotalCount(), page, size);
        return result;
    }
}
